package org.ruiyichen.guizhou.agr.base;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.net.OkHttpManager;
import org.ruiyichen.guizhou.agr.utils.L;
import org.ruiyichen.guizhou.agr.utils.StrUtil;

import java.util.Observable;
import java.util.Observer;

import androidx.annotation.DrawableRes;
import androidx.fragment.app.Fragment;

/**
 * 基础Fragment
 */
public abstract class BaseFragment extends Fragment implements Observer {

    protected BaseActivity mActivity;

    protected OkHttpManager bOkHttpManager;

    protected View mRootView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRetainInstance(true);

        mActivity = (BaseActivity) getActivity();

        //绑定数据管理类
        DataManger.getInstance().addObserver(this);
    }

    public BaseFragment setTitle(String title) {
        return setTitle(title, false);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    protected Handler bHandler = new Handler() {

        @Override
        public void handleMessage(android.os.Message msg) {
            L.v("receive msg.what = " + msg.what + " msg.arg2 = " + msg.arg2 + " msg.obj = " + msg.obj);
            // 如果是从缓存取数据
//            loadingDialog.dismiss();
            L.e("simpleName:" + getActivity().getClass().getSimpleName());
            if (((BaseActivity) getActivity()).loadingDialog.isShowing()) {
                ((BaseActivity) getActivity()).loadingDialog.dismiss();
            }
            if (msg.arg2 == 99) {
                onPostHandle(msg.what, msg.obj, false, 0, null, null);
                return;
            }
            if (msg.what == 404) {
                onPostHandle(msg.what, msg.obj, true, 0, null, null);
                return;
            }
            if (null != msg.obj) {
                onPostHandle(msg.what, msg.obj, false, 0, null, null);
            } else {
                onPostHandle(msg.what, msg.obj, false, 0, null, null);
            }
        }
    };

    /**
     * <一句话功能简述> <功能详细描述>
     *
     * @param requestType 请求标示
     * @param response    返回的报文
     * @param error       是否请求失败， false为失败，true为成功
     * @param errorCode   错误标示码
     * @param obj1        备用字段1
     * @param obj2        备用字段2 最后更新时间：<date> 修改履历：<修改内容>
     */
    protected void onPostHandle(int requestType, Object response, boolean error, int errorCode, Object obj1, Object obj2) {
    }


    @Override
    public void update(Observable observable, Object data) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DataManger.getInstance().deleteObserver(this);
    }

    public abstract boolean onBackPressed();

    @Override
    public void startActivity(Intent intent) {
        getActivity().startActivity(intent);
    }

    public void refreshUI() {

    }

    /**
     * 跳转首页
     *
     * @param page 跳转首页
     */
    public void goHomePage(int page) {
        ((BaseActivity) getActivity()).goHomePage(page);
    }

//    protected BaseActivity getCustomActivity() {
//        return (BaseActivity) getActivity();
//    }


    // 设置标题
    public BaseFragment setTitle(String title, boolean canBack) {
        try {
            if (StrUtil.isNotEmpty(title)) {
                mRootView.findViewById(R.id.rl_normal_title).setVisibility(View.VISIBLE);
                ((TextView) mRootView.findViewById(R.id.tv_normal_title)).setText(title);
            } else {
                mRootView.findViewById(R.id.rl_normal_title).setVisibility(View.GONE);
            }
//        findViewById(R.id.iv_normal_setting).setVisibility(View.GONE);
//        findViewById(R.id.tv_normal_save).setVisibility(View.GONE);
            if (canBack) {
                mRootView.findViewById(R.id.iv_normal_back).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onBackPressed();
                    }
                });
            }
        } catch (Exception e) {
            L.e(e.getMessage());
        }
        return this;
    }

    // 设置
    public BaseFragment setRightImage(@DrawableRes int rightRes) {
        try {
            TextView normalOther = ((TextView) mRootView.findViewById(R.id.tv_normal_other));
            if (rightRes != 0) {
                normalOther.setCompoundDrawablesWithIntrinsicBounds(0, 0, rightRes, 0);
            } else {
                normalOther.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            }
            boolean isVisibility = rightRes != 0 || StrUtil.isNotEmpty(normalOther.getText()
                    .toString());
            normalOther.setVisibility(isVisibility ? View.VISIBLE : View.GONE);
        } catch (Exception e) {
            L.e(e.getMessage());
        }
        return this;
    }

    // 设置
    public BaseFragment setRightText(String rightText) {
        try {
            TextView normalOther = ((TextView) mRootView.findViewById(R.id.tv_normal_other));
            if (StrUtil.isNotEmpty(rightText)) {
                normalOther.setText(rightText);
            } else {
                normalOther.setText(rightText);
            }
            Drawable[] drawables = normalOther.getCompoundDrawables();
            boolean hasDrawable = false;
            for (Drawable drawable : drawables) {
                if (drawable != null) {
                    hasDrawable = true;
                    break;
                }
            }
            boolean isVisibility = hasDrawable || StrUtil.isNotEmpty(rightText);
            normalOther.setVisibility(isVisibility ? View.VISIBLE : View.GONE);
        } catch (Exception e) {
            L.e(e.getMessage());
        }
        return this;
    }
}
